<?php header("Content-Type: text/html;charset=utf-8"); 
require_once "comm.php";
require_once "helper.php";
$page = $_GET['curpage'];
$double = $_GET['double'];//单字1,双字2
$sex = $_GET['sex'];//男1,女2
$mode = $_GET['mode'];//1为五行，2为综合。
//echo mb_substr($_GET['lastname'],mb_strlen($_GET['lastname'],"UTF8")-1,1,"UTF8");
$lastname = addslashes(mb_substr($_GET['lastname'],mb_strlen($_GET['lastname'],"UTF8")-1,1,"UTF8"));
//echo $lastname;
$shengxiaoNan=array("子"=>94, "丑"=>96, "寅"=>98, "卯"=>92, "辰"=>99, "巳"=>98, "午"=>98, "未"=>88, "申"=>94, "酉"=>94, "戌"=>96, "亥"=>95);
$shengxiaoNv=array("子"=>94, "丑"=>96, "寅"=>88, "卯"=>98, "辰"=>99, "巳"=>97, "午"=>98, "未"=>88, "申"=>94, "酉"=>92, "戌"=>96, "亥"=>95);
//echo $shengxiaoNv['寅'];
$dafenUrl="https://qmsq.7dar.com/dafenname?lastname=".urlencode('于')."&firstname=".urlencode('小青')."&sex=".$sex."&birthdaydate=".urlencode($_GET['birthdaydate'])."&birthdaytime=".urlencode($_GET['birthdaytime'])."&refreshCount=0&openid=oP9Xs0BQB-AgozoBPB0oK-sum2dE&nickname=seeker&unionid=oyHkowRi3WUArLGQkFnEhKJEJOvw";
$dafenname=json_decode(http_get($dafenUrl)["content"]);
$fenShengxiao=95;
$fenBZWX=80;
if($sex==1)
{$fenShengxiao = $shengxiaoNan[$dafenname -> baziInfo -> dizhi];}
else
{$fenShengxiao = $shengxiaoNv[$dafenname -> baziInfo -> dizhi];}
//echo $dafenname -> baziInfo -> dizhi;
$xyspy=$dafenname -> wuxingInfo -> xishen ->xiyongshen;//需要新增八字五行的分数
//
function getFontFTBihua($font)
{
	$reVal =1;
	$conn1=@mysql_connect('118.178.134.47:3000','qmsqapi','qmsqapi!9234@#$ss%^&*&UKL()');//连接数据库
  if (!$conn1)
  {
	echo 'Could not connect: ';
  die('Could not connect: ' . mysql_error());
  }
mysql_set_charset("utf8", $conn1);
mysql_query("set names utf8;");
$db_selected = mysql_select_db("qiming", $conn1);
if (!$db_selected)
  {
  die ("Can\'t use test_db : " . mysql_error());
  }
$sql="select ftbihua from qiming.fontbase where font='".($font)."' limit 1;";
//echo $sql;
  $weixininfo1=mysql_query($sql,$conn1);//创建sql语句
  while($row=mysql_fetch_assoc($weixininfo1))//获取结果集每一行的数据
  { 
	$reVal =$row['ftbihua'];
  }
  //mysql_free_result($weixininfo1);//释放结果集
  //mysql_close($conn1); //关闭数据库
  return $reVal;
}
if($_SERVER["SERVER_PORT"] != '8080')
{
	$conn=@mysql_connect('118.178.134.47:3000','qmsqapi','qmsqapi!9234@#$ss%^&*&UKL()');//连接数据库
}
else
{
	$conn=@mysql_connect('127.0.0.1:4444','root','usbw');//连接数据库
}
  if (!$conn)
  {
	echo 'Could not connect: ';
  die('Could not connect: ' . mysql_error());
  }
mysql_set_charset("utf8", $conn);
mysql_query("set names utf8;");
$db_selected = mysql_select_db("qiming", $conn);
if (!$db_selected)
  {
  die ("Can\'t use test_db : " . mysql_error());
  }
  $sqlxys="";
  //echo $mode;
  if($mode==1)
  { 
	$sqlxys=" and m.".$xyspy."fen>80 ";
  }
  $countsql = "select count(*)
 from qimingcache.paiming_".getFontFTBihua($lastname)." p join qiming.multi_name m on(p.nameid=m.id) 
  where m.sex=".$sex." and m.num=".$double.$sqlxys
      ." order by p.nameid ";
$query = mysql_query($countsql,$conn);//创建sql语句
$count=mysql_fetch_assoc($query)['count(*)'];

$pages = $count/10;
$pages = floor($pages);

if($pages!=0){
    $page = $page%$pages;
}
if($page==0){
    $page = $pages;
}else{
    $page = $page;
}
$sql="select distinct yxy*0.6+wuge*0.1+zygx*0.1+m.".$xyspy."fen*0.15 as score,m.name,p.nameid as id,m.zongfen
 from qimingcache.paiming_".getFontFTBihua($lastname)." p join qiming.multi_name m on(p.nameid=m.id) 
  where m.sex=".$sex." and m.num=".$double.$sqlxys
//  ." order by yxy*0.6+wuge*0.1+zygx*0.1+p.".$xyspy."*0.15  desc,p.nameid ".
  ." order by p.nameid ".
" limit ".(($page-1)*10).",10;";
//echo $sql;
  $weixininfo=mysql_query($sql,$conn);//创建sql语句
$rowsval='';
  while($row=mysql_fetch_assoc($weixininfo))//获取结果集每一行的数据
  {
  $allscore=round(floatval($row['score'])+$fenShengxiao*0.05);
  //echo $allscore;
  if($allscore>99){$allscore=99;}
	$rowsval=$rowsval.'{"id":'.$row['id'].',"name":"'.$row['name'].'","score":'.$allscore.'},';
  }
  mysql_free_result($weixininfo);//释放结果集
  mysql_close($conn); //关闭数据库
  $reVal='{"namesGroup":['.trim($rowsval,',').'],"success":"true","message":"OK"}';
  echo $reVal;
?>